package com.tzinfo.tzb;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.galhttprequest.GalHttpRequest;
import com.galhttprequest.GalHttpRequest.GalHttpLoadTextCallBack;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.tzinfo.data.TztData;
import com.tzinfo.tzb.domain.DefaultResult;
import com.tzinfo.tzb.domain.DiaryModel;
import com.tzinfo.tzb.domain.DiaryList.DiaryData;
import com.tzinfo.tzb.util.TzbCommonData;
import com.yxw.util.OutLog;
import com.yxw.util.YXWDateUtil;
import com.yxw.view.util.ResFinder;

public class TzbMyDirayDetailActivity extends TzbBaseActivity {

	public static TzbMyDirayDetailActivity activity;
	// 标志
	private boolean isMine = false;
	private boolean isChanged = false;
	private boolean isNeedReflash = false;
	private int dtype = 0;
	private int flag = 0;

	// 参数

	// UI组件
	private Button bt_topbar_r1;
	private TextView text_diary_title;
	private TextView text_diary_context;
	private TextView text_diary_time;
	private TextView text_diary_supports;
	private TextView bt_title_good;
	private ImageView img_tiezi_tupian;

	// 数据
	private DefaultResult result = null;
	public DiaryData diaryData = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(ResFinder.getR(this,
				"R.layout.tzb_ac_main_my_diraydetail"));
		activity = this;
		initParameter();
		init();
	}

	private void getDirayData() {
		switch (flag) {
		case 0:
			diaryData = TzbCommonData.diaryList.getData().get(
					TzbCommonData.diaryItem);
			break;
		case 1:
			diaryData = TzbDirayListActivity.diaryList.getData().get(
					TzbCommonData.diaryItem);
			break;
		default:
			break;
		}
	}

	private void initParameter() {
		flag = this.getIntent().getIntExtra("flag", 0);
		getDirayData();
		// 判断是否为本人
		if (diaryData != null) {
			if (TztData.userInfo != null) {
				String username = diaryData.getUsername();
				String myusername = TztData.userInfo.getData().getUserName();
				OutLog.LogE("myusername", myusername);
				OutLog.LogE("username", username);
				if (myusername != null && username != null
						&& username.trim().equals(myusername.trim())) {
					isMine = true;
				}
			}
		}
	}

	private void init() {
		// 加载滚动进入条
		mProgressBar = (ProgressBar) ResFinder.findViewById(this,
				"R.id.progressbar_loding");
		text_diary_title = (TextView) ResFinder.findViewById(this,
				"R.id.text_diary_title");
		text_diary_context = (TextView) ResFinder.findViewById(this,
				"R.id.text_diary_context");
		text_diary_time = (TextView) ResFinder.findViewById(this,
				"R.id.text_diary_time");
		text_diary_supports = (TextView) ResFinder.findViewById(this,
				"R.id.text_diary_supports");
		bt_title_good = (TextView) ResFinder.findViewById(this,
				"R.id.bt_title_good");
		bt_topbar_r1 = (Button) ResFinder.findViewById(this,
				"R.id.bt_topbar_r1");
		img_tiezi_tupian = (ImageView) ResFinder.findViewById(this,
				"R.id.img_tiezi_tupian");

		if (!isMine) {
			bt_topbar_r1.setVisibility(View.GONE);
		} else {
			bt_topbar_r1.setVisibility(View.VISIBLE);
		}
		setInfo();
	}

	private void setInfo() {
		getDirayData();
		// 初始化
		if (diaryData != null) {
			text_diary_title.setText(diaryData.getTitle() + "");
			text_diary_context.setText(diaryData.getMessage() + "");
			text_diary_time.setText(YXWDateUtil.getTimeByDayInterval(
					diaryData.getPtime(), "MM/dd/yyyy"));
			text_diary_supports.setText("(" + diaryData.getSupports() + ")");
			if (diaryData.getPhoto() != null && diaryData.getPhoto().size() > 0) {
				imageLoader.displayImage(diaryData.getPhoto().get(0)
						.getPicfile(), img_tiezi_tupian, options);
			}
		}
	}

	public void supportsDiray(View v) {
		dialog.show();
		String url = "";
		url = ResFinder.getResString(this, "R.string.tzb_url_DiarySup")
				+ "?diaryid=" + diaryData.getId() + "&type=" + dtype;
		OutLog.LogE("yxw", url);
		GalHttpRequest galHttpRequest = new GalHttpRequest(this);
		galHttpRequest.setCacheEnable(false);
		galHttpRequest.setURL(url);
		galHttpRequest.startAsynRequestString(new GalHttpLoadTextCallBack() {

			@Override
			public void textLoaded(String arg0) {
				OutLog.LogE("yxw", "json:" + arg0);
				Gson gson = new Gson();
				java.lang.reflect.Type type = new TypeToken<DefaultResult>() {
				}.getType();
				result = gson.fromJson(arg0, type);
				OutLog.LogE("yxw", "result:" + result.toString());
				dialog.dismiss();
				if (result.isSuccess()) {
					if (dtype == 0) {
						Toast.makeText(TzbMyDirayDetailActivity.this,
								"你赞了这篇日志！", 1).show();
						dtype = 1;
						diaryData.setSupports(diaryData.getSupports() + 1);
						text_diary_supports.setText("("
								+ diaryData.getSupports() + ")");
						bt_title_good.setText("取消赞");
					} else {
						Toast.makeText(TzbMyDirayDetailActivity.this, "你取消了赞！",
								1).show();
						dtype = 0;
						diaryData.setSupports(diaryData.getSupports() - 1);
						text_diary_supports.setText("("
								+ diaryData.getSupports() + ")");
						bt_title_good.setText("点赞");
					}
					isChanged = true;
				} else {
					Toast.makeText(TzbMyDirayDetailActivity.this,
							"抱歉，数据请求失败，请重试！", 1).show();
				}

			}
		});
	}

	@Override
	public void goback(View view) {
		Intent intent = new Intent();
		intent.putExtra(TzbCommonData.IS_OK, isChanged);
		intent.putExtra(TzbCommonData.IS_NEED_REFLASH, isNeedReflash);
		TzbTabMyActivity.isNeedReflash = isChanged;
		setResult(1, intent);
		this.finish();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			goback(null);
			return false;
		} else {
			return super.onKeyDown(keyCode, event);
		}
	}

	private void requestInfo() {
		showProgressBar();
		GalHttpRequest galHttpRequest = new GalHttpRequest(this);
		galHttpRequest.setCacheEnable(false);
		String url = ResFinder
				.getResString(this, "R.string.tzb_url_DiaryModel")
				+ "?diaryid=" + diaryData.getId();
		galHttpRequest.setURL(url);
		galHttpRequest.startAsynRequestString(new GalHttpLoadTextCallBack() {

			@Override
			public void textLoaded(String arg0) {
				OutLog.LogE("yxw", "json:" + arg0);
				Gson gson = new Gson();
				java.lang.reflect.Type type = new TypeToken<DiaryModel>() {
				}.getType();
				DiaryModel diaryModel = gson.fromJson(arg0, type);
				if (diaryModel.isSuccess()) {
					diaryData = diaryModel.getData();
					flag=-1;
					setInfo();
				}
				removeProgressBar();
			}
		});
	}

	// onclick
	public void goEdit(View v) {
		Intent intent = new Intent(this, TzbMyDirayWriteActivity.class);
		intent.putExtra("isPub", false);
		intent.putExtra("flag", flag);
		startActivityForResult(intent, 1);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == 1) {
			if (resultCode == 1) {
				boolean isOldChanged = data.getBooleanExtra(
						TzbCommonData.IS_OK, false);
				boolean isNeedReflash = data.getBooleanExtra(
						TzbCommonData.IS_NEED_REFLASH, false);
				boolean isDelete = data.getBooleanExtra(
						TzbCommonData.IS_DELETE, false);
				isChanged = isOldChanged;
				this.isNeedReflash = isNeedReflash;
				if (isDelete) {
					goback(null);
				}
				if (isChanged) {
					if (isNeedReflash) {
						requestInfo();
					} else {
						setInfo();
					}
				}
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
}
